package org.skydiveapp2;

import org.skydiveapplibrary.ui.DrawingView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class LogEntryDiagramActivity extends Activity
{	
	@Override 
	public void onCreate(Bundle savedInstanceState)
    { 
        super.onCreate(savedInstanceState);

        overridePendingTransition(R.anim.fadein,
                R.anim.hold);
        
        setContentView(R.layout.log_entry_diagram_view);
        
        // init header
        setSubtitleText(R.string.log_entry_diagram_header);
        
        // get drawingview 
        DrawingView drawingView = (DrawingView) findViewById(R.id.LogEntryDiagramDrawingView);
        // load data
        byte[] data = getIntent().getByteArrayExtra(Constants.EXTRA_DIAGRAM);
        if (data != null)
        {
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                drawingView.drawBitmap(bitmap);
        }
    }
	
	private void clear()
	{
		DrawingView drawingView = (DrawingView) findViewById(R.id.LogEntryDiagramDrawingView);
		drawingView.clear();
	}
	
	private void save()
    {
            DrawingView drawingView = (DrawingView)findViewById(R.id.LogEntryDiagramDrawingView);
            
            if (!drawingView.isBitmapChanged())
            {
                    // cancelled if not modified
                    setResult(RESULT_CANCELED);
            }
            else
            {
                    // get image data
                    byte[] data = drawingView.getImageData();
                    // set result intent
                    Intent intent = new Intent();
                    intent.putExtra(Constants.EXTRA_DIAGRAM, data);
                    setResult(RESULT_OK, intent);
            }

            // finish
            finish();
    }
	
	private void setSubtitleText(int id)
	{
		// set subtitle label
		TextView subtitleTextView = (TextView) getActionBar().getCustomView().findViewById(R.id.actionbarSubtitle);
		subtitleTextView.setText(id);
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu_save, menu);
    	inflater.inflate(R.menu.menu_clear, menu);
    	inflater.inflate(R.menu.menu_back, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menu_save:
    		save();
            return true;
            
        case R.id.menu_back:
			close();
            
        case R.id.menu_clear:
        	clear();
            return true;
            
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
	@Override
    public void onBackPressed() {
		close();
    }
	
	private void close()
	{
		finish();
    	overridePendingTransition(R.anim.hold,
                R.anim.fadeout);
	}
}
